﻿using Kimd.Common;
using Kimd.Communication;
using Kimd.Machine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;
using WorkStation.UI.Controls;

namespace WorkStation.UI
{
    public partial class FormDevice : Form, IUserChange
    {
        private FormMain _mainForm;
        public FormDevice(FormMain mainForm)
        {
            InitializeComponent();
            this._mainForm = mainForm;
        }

        private void FormDevice_Load(object sender, EventArgs e)
        {
            int index = 0;
            foreach(var item in MachineResource.Instance.MachineTcpClient)
            {
                if (item.Key.Contains("治具"))
                    continue;
                CreateDevice(item.Key, item.Key, index, item.Value);
                index++;
                item.Value.Connected += FormDevice_Connected;
                item.Value.Disconnected += FormDevice_Disconnected;
            }

            foreach (var item in MachineResource.Instance.MachineTcpServer)
            {
                CreateDevice(item.Key, item.Key, index, item.Value);
                index++;
            }

            foreach (var item in MachineResource.Instance.MachinePLC)
            {
                CreateDevice(item.Key, item.Key, index, item.Value);
                index++;
            }

            foreach (var item in this.panelEx3.Controls)
            {
                if (item is DeviceModule)
                {
                    (item as DeviceModule).OnDeviceSelected += FormDevice_OnDeviceSelected;
                    (item as DeviceModule).OnStatusChanged += FormDevice_OnStatusChanged;
                }
            }
        }
        private void OnClientConnected(string info)
        {
            this.BeginInvoke(new Action(() =>
            {
                SystemInfo.ShowToastInfo(this, info, false);
            }));
        }

        private void FormDevice_Connected(object sender, TcpClientStateEventArgs e)
        {
            string clientName = (sender as ICommunicationShow).Name;
            foreach (var item in this.panelEx3.Controls)
            {
                if (item is DeviceModule)
                {
                    if ((item as DeviceModule).Tag.ToString() == clientName)
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            (item as DeviceModule).Status = "已连接";
                        }));
                    }
                }
            }
        }

        private void FormDevice_Disconnected(object sender, TcpClientStateEventArgs e)
        {
            string clientName = (sender as ICommunicationShow).Name;
            foreach (var item in this.panelEx3.Controls)
            {
                if (item is DeviceModule)
                {
                    if ((item as DeviceModule).Tag.ToString() == clientName)
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            (item as DeviceModule).Status = "未连接";
                        }));
                    }
                }
            }
        }

        private void FormDevice_OnStatusChanged(object sender, string message)
        {
            SystemInfo.ShowToastInfo(this, $"{message}", true);
        }

        private void FormDevice_OnDeviceSelected(object sender, EventArgs e)
        {
            foreach (var item in panelEx1.Controls)
            {
                if (item is Form)
                {
                    (item as Form).Close();
                }
            }
            panelEx1.Controls.Clear();

            Form form = (sender as DeviceModule).GetDeviceForm();
            if(form is CommunicationPage)
                (form as CommunicationPage).NotifyCmdChanged = CommandChanged;
            
            if (form != null)
            {
                if (!panelEx1.Controls.Contains(form))
                {
                    form.TopLevel = false;
                    form.Dock = DockStyle.Fill;
                    form.FormBorderStyle = FormBorderStyle.None;
                    panelEx1.Controls.Add(form);
                }
                form.Show();
            }
        }

        private void FormDevice_Resize(object sender, EventArgs e)
        {
        }
        private void CommandChanged(ICommunicationShow device, SerializableDictionary<string, string> cmds)
        {
            if (device is TCPClient)
            {
                MachineResource.Instance.MachineTcpClient[device.Name].Commands = cmds;
                MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
            }
        }

        public void CreateDevice(string name, string description, int index, ICommunicationShow client)
        {
            DeviceModule newDevice = new DeviceModule();
            newDevice.Description = description;
            newDevice.Info = client.GetTreeNodeShowStr();
            if (index < 9)
                newDevice.Location = new System.Drawing.Point(12 + 145 * index, 6);
            else
                newDevice.Location = new Point(12 + 145 * (index - 9), 6 + 110);
            newDevice.Name = name;
            newDevice.Tag = description;
            newDevice.SelectedColor = System.Drawing.Color.Empty;
            newDevice.Size = new System.Drawing.Size(140, 110);
            newDevice.Status = client.IsOpenFlag ? "已连接" : "未连接";
            newDevice.TabIndex = index;

            newDevice.SetOperator(client);

            this.panelEx3.Controls.Add(newDevice);
        }

        public void UserChanged()
        {

        }
    }
}
